package com.devbobcorn.nekoration.client.rendering.entities;

import com.google.common.base.MoreObjects;
import com.mojang.blaze3d.matrix.MatrixStack;

import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.player.AbstractClientPlayerEntity;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.item.CrossbowItem;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.Hand;
import net.minecraft.util.HandSide;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.vector.Vector3f;

public class ElectroHandRenderer {
    private final ElectroRenderer renderer;

    private float mainHandHeight;
    private float oMainHandHeight;
    private float offHandHeight;
    private float oOffHandHeight;

    public ElectroHandRenderer(ElectroRenderer parentRenderer){
        this.renderer = parentRenderer;
    }

    public void render(float partialTicks, MatrixStack stack, IRenderTypeBuffer buffers, ClientPlayerEntity player, int packedLight) {
        float f = player.getAttackAnim(partialTicks);
        Hand hand = MoreObjects.firstNonNull(player.swingingArm, Hand.MAIN_HAND);
        float f1 = MathHelper.lerp(partialTicks, player.xRotO, player.xRot);
        boolean flag = true;
        boolean flag1 = true;
        if (player.isUsingItem()) {
            ItemStack itemstack = player.getUseItem();
            if (itemstack.getItem() instanceof net.minecraft.item.ShootableItem) {
                flag = player.getUsedItemHand() == Hand.MAIN_HAND;
                flag1 = !flag;
            }
            Hand hand1 = player.getUsedItemHand();
            if (hand1 == Hand.MAIN_HAND) {
                ItemStack itemstack1 = player.getOffhandItem();
                if (itemstack1.getItem() == Items.CROSSBOW && CrossbowItem.isCharged(itemstack1)) {
                    flag1 = false;
                }
            }
        } else {
            ItemStack itemstack2 = player.getMainHandItem();
            ItemStack itemstack3 = player.getOffhandItem();
            if (itemstack2.getItem() == Items.CROSSBOW && CrossbowItem.isCharged(itemstack2)) {
                flag1 = !flag;
            }
            if (itemstack3.getItem() == Items.CROSSBOW && CrossbowItem.isCharged(itemstack3)) {
                flag = !itemstack2.isEmpty();
                flag1 = !flag;
            }
        }
        float f3 = MathHelper.lerp(partialTicks, player.xBobO, player.xBob);
        float f4 = MathHelper.lerp(partialTicks, player.yBobO, player.yBob);
        stack.mulPose(Vector3f.XP.rotationDegrees((player.getViewXRot(partialTicks) - f3) * 0.1F));
        stack.mulPose(Vector3f.YP.rotationDegrees((player.getViewYRot(partialTicks) - f4) * 0.1F));

        if (flag) {
            float f5 = hand == Hand.MAIN_HAND ? f : 0.0F;
            float f2 = 1.0F - MathHelper.lerp(partialTicks, this.oMainHandHeight, this.mainHandHeight);
            this.renderArmElectroWithItem(player, partialTicks, f1, Hand.MAIN_HAND, f5, player.getMainHandItem(), f2, stack, buffers, packedLight);
        }
        if (flag1) {
            float f6 = hand == Hand.OFF_HAND ? f : 0.0F;
            float f7 = 1.0F - MathHelper.lerp(partialTicks, this.oOffHandHeight, this.offHandHeight);
            this.renderArmElectroWithItem(player, partialTicks, f1, Hand.OFF_HAND, f6, player.getOffhandItem(), f7, stack, buffers, packedLight);
        }
    }

    private void renderArmElectroWithItem(AbstractClientPlayerEntity player, float partialTicks, float b, Hand hand, float c, ItemStack item, float d, MatrixStack stack, IRenderTypeBuffer buffers, int packedLight) {
        boolean flag = hand == Hand.MAIN_HAND;
        HandSide handside = flag ? player.getMainArm() : player.getMainArm().getOpposite();
        stack.pushPose();
        if (flag && !player.isInvisible()){
            if (item.isEmpty()) {
                this.renderPlayerArmElectro(partialTicks, stack, buffers, packedLight, d, c, handside);
            } else if (item.getItem() == Items.FILLED_MAP) {
                // Map Hand...
            } else if (item.getItem() == Items.CROSSBOW) {
                // Crossbow Hand...
            }
        }
        stack.popPose();
    }

    @SuppressWarnings("resource")
    private void renderPlayerArmElectro(float partialTicks, MatrixStack stack, IRenderTypeBuffer buffers, int packedLight, float a, float b, HandSide handSide) {
        boolean flag = handSide != HandSide.LEFT;
        float f = flag ? 1.0F : -1.0F;
        float f1 = MathHelper.sqrt(b);
        float f2 = -0.3F * MathHelper.sin(f1 * (float) Math.PI);
        float f3 = 0.4F * MathHelper.sin(f1 * ((float) Math.PI * 2F));
        float f4 = -0.4F * MathHelper.sin(b * (float) Math.PI);
        stack.translate((double) (f * (f2 + 0.64000005F)), (double) (f3 + -0.6F + a * -0.6F), (double) (f4 + -0.71999997F));
        stack.mulPose(Vector3f.YP.rotationDegrees(f * 45.0F));
        float f5 = MathHelper.sin(b * b * (float) Math.PI);
        float f6 = MathHelper.sin(f1 * (float) Math.PI);
        stack.mulPose(Vector3f.YP.rotationDegrees(f * f6 * 70.0F));
        stack.mulPose(Vector3f.ZP.rotationDegrees(f * f5 * -20.0F));
        AbstractClientPlayerEntity player = Minecraft.getInstance().player;
        Minecraft.getInstance().getTextureManager().bind(ElectroRenderer.MISAKA_ELECTRO);
        stack.translate((double) (f * -1.0F), (double) 3.6F, 3.5D);
        stack.mulPose(Vector3f.ZP.rotationDegrees(f * 120.0F));
        stack.mulPose(Vector3f.XP.rotationDegrees(200.0F));
        stack.mulPose(Vector3f.YP.rotationDegrees(f * -135.0F));
        stack.translate((double) (f * 5.6F), 0.0D, 0.0D);
        if (flag) {
            renderer.renderRightHand(partialTicks, stack, buffers, packedLight, player);
        } else {
            renderer.renderLeftHand(partialTicks, stack, buffers, packedLight, player);
        }
    }
}
